﻿using System;
using System.Collections.Generic;
using System.Linq;
using SSG.Tools;

namespace SSG.Model
{
    /// <summary>
    /// STAR
    /// </summary>
    public class Approach : Procedure
    {

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="name"></param>
        /// <param name="airport"></param>
        /// <param name="runway"></param>
        public Approach(string name, Airport airport, Runway runway) : base(name, airport, runway)
        {
            // code
        }

        /// <summary>
        /// Guess the corresponding runway by its name.
        /// </summary>
        /// <param name="runways"></param>
        /// <returns></returns>
        public Runway GuessRunway(List<Runway> runways)
        {
            if (runways.NullSafeIsEmpty()) return null;
            if (string.IsNullOrEmpty(this.Name)) return null;
            return runways.FirstOrDefault(rw => this.Name.Contains(rw.Name));
        }

        /// <summary>
        /// Add transition
        /// </summary>
        /// <param name="transition"></param>
        public new void AddTransition(Transition transition)
        {
            base.AddTransition(transition);
            if (transition.Approach == null) transition.Approach = this;
            if (transition.Approach != this) throw new InvalidOperationException("Transition " + transition + " already has different approach set " + transition.Approach);
        }

        /// <summary>
        /// ESE format.
        /// </summary>
        /// <summary></summary>
        /// <param name="settings"></param>
        /// <returns></returns>
        public override string ToEse(GeneratorSettings settings = null)
        {
            return base.ToEse("APPROACH:", settings);
        }
    }
}